/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzilla.desk;

import com.yy.bugzilla.desk.tool.BugListExport;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.Tab;

/**
 *
 * @author Alan
 */
public class MainMenu {

    private static final Logger logger = Logger.getLogger(MainMenu.class.getName());
    private MenuBar menuBar;
    private MainApp mainApp;

    public MainMenu(MainApp mainApp) {
        this.mainApp = mainApp;
    }

    public MenuBar getMenuBar() {
        if (menuBar == null) {
            menuBar = generateMenuBar();
        }
        return menuBar;
    }

    private MenuBar generateMenuBar() {
        MenuBar bar = new MenuBar();
        bar.getMenus().addAll(getFileMenu(), getBugListMenu(), getHelpMenu());
        return bar;
    }

    private Menu getFileMenu() {
        Menu m = new Menu("File");
        MenuItem exit = new MenuItem("Exit");
        exit.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                Platform.exit();
            }
        });
        m.getItems().add(exit);
        return m;
    }

    private Menu getBugListMenu() {
        Menu m = new Menu("Bug List");
        MenuItem exit = new MenuItem("Export");
        exit.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent t) {
                doExportBugList();
            }
        });
        m.getItems().add(exit);
        return m;
    }

    private Menu getHelpMenu() {
        Menu m = new Menu("Help");
        m.getItems().add(new MenuItem("About"));
        return m;
    }

    private void doExportBugList() {
        MainTabPane tabPane = mainApp.getMainTabPane();
        Tab selectedTab = tabPane.getSelectionModel().getSelectedItem();
        if (selectedTab != null) {
            BugListPane bugListPane = tabPane.getBugListPane(selectedTab.getText());
            if (bugListPane != null) {
                String export = BugListExport.export(bugListPane.getBugDetailDatas());
                System.out.println("export=" + export);
            } else {
                logger.log(Level.INFO, "The selected tab is not a bug list pane!");
            }
        } else {
            logger.log(Level.INFO, "Have not select a tab!");
        }
    }
}
